package com.sctvcloud.zhaohua_sd.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.TitlePagerChangeListener;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.base.BaseActivity;
import com.sctvcloud.zhaohua_sd.beans.FSubscribe;
import com.sctvcloud.zhaohua_sd.beans.HomeListItem;
import com.sctvcloud.zhaohua_sd.beans.LiveHomeAdvance;
import com.sctvcloud.zhaohua_sd.beans.NewsItem;
import com.sctvcloud.zhaohua_sd.ui.activities.LivingActivity;
import com.sctvcloud.zhaohua_sd.ui.utils.GlideUtil;
import com.sctvcloud.zhaohua_sd.ui.utils.SkipUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GovBannerHolder extends BaseAbsHolder<HomeListItem> implements IAndroidLittleLife {
    @BindView(R.id.news_convenientBanner)
    protected ConvenientBanner banner;
    @BindView(R.id.iv_banner)
    protected CustomEXImageView ivPic;
    @BindView(R.id.title_bg)
    protected ImageView titleBg;
    @BindView(R.id.left_title)
    protected TextView leftTitle;
    @BindView(R.id.tv_banner_title)
    protected CustomFontTextView tvTitle;
    private List<NewsItem> channelBeanList;
    private Context context;

    private boolean hasInManager;

    private int lifeMode = 0;

    public GovBannerHolder(Context context, View itemView) {
        super(context, itemView);
        this.context = context;
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = width * 9 / 16;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) banner.getLayoutParams();
        params.width = width;
        params.height = height;
        banner.setLayoutParams(params);
        banner.setIs169(true);
        banner.setBottomWhite(context.getResources().getColor(R.color.no_white));
        banner.setBottomBg(R.mipmap.banner_bottom_bg, false);

    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }


    @Override
    public void setData(final HomeListItem item) {
//        initListDataAndAction();
        if (item == null || !ListUtils.isListValued(item.getNewsItemList())) {
            itemView.setVisibility(View.GONE);
        } else {
            if (itemView.getVisibility() != View.VISIBLE) {
                itemView.setVisibility(View.VISIBLE);
            }
            if (item.getNewsItemList() == channelBeanList) {
                return;
            }
            if (item.getNewsItemList().size() == 1) {
                banner.setVisibility(View.GONE);
                ivPic.setVisibility(View.VISIBLE);
                tvTitle.setVisibility(View.GONE);
//                tvTitle.setText(item.getNewsItemList().get(0).getNewsTitle());
                GlideUtil.loadImgWith83Def(context, item.getNewsItemList().get(0).getNewsImage(), ivPic);
                ivPic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBannerItemClick(item.getNewsItemList().get(0));
                    }
                });
                leftTitle.setVisibility(View.GONE);
//                leftTitle.setText("预告");
                titleBg.setVisibility(View.GONE);
            } else {
                leftTitle.setVisibility(View.GONE);
                titleBg.setVisibility(View.GONE);
                banner.setVisibility(View.VISIBLE);
                tvTitle.setVisibility(View.GONE);
                ivPic.setVisibility(View.GONE);
//                flowView.setPages(item.getNewsItemList(), new MZHolderCreator() {
//                    @Override
//                    public MZViewHolder createViewHolder() {
//                        return new MzBannerHolder(item.getNewsItemList().size());
//                    }
//                });
//                JLog.e("aaa will call flow start");
//                flowView.start();
                banner.setTitlePagerChangeListener(new TitlePagerChangeListener<NewsItem>() {
                    @Override
                    public void onTitlePagetChange(TextView title, int postiton, NewsItem data) {
//                        if (data != null && !TextUtils.isEmpty(data.getDataTitle())) {
//                            title.setVisibility(View.VISIBLE);
//                            title.setText(data.getDataTitle());
//                        }
                    }
                    @Override
                    public void onTitlePagetChange(TextView leftTitle, TextView title, int postiton, NewsItem data) {

                    }
                });
                banner.setPages(new CBViewHolderCreator() {
                    @Override
                    public Object createHolder() {
                        return new BannerHolder();
                    }
                }, item.getNewsItemList())
                        //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                        .setPageIndicator(new int[]{R.mipmap.banner_icon_normal, R.mipmap.banner_icon_down})
                        //设置指示器的方向
                        .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
                banner.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        NewsItem channelBean = channelBeanList.get(position);
                        onBannerItemClick(channelBean);
                    }
                });
                if (!banner.isTurning()) {
                    banner.startTurning(3000);
                }


            }
            channelBeanList = item.getNewsItemList();
        }
    }


//    private MZBannerView.BannerPageClickListener bannerPageClickListener = new MZBannerView.BannerPageClickListener() {
//        @Override
//        public void onPageClick(View view, int position) {
//            NewsItem channelBean = channelBeanList.get(position);
////            Toast.makeText(context, "点击事件 position：" + position
////                    + "， text：" + channelBean.getNewsTitle(), Toast.LENGTH_SHORT).show();
////            context.startActivity(new Intent(context, NewesDetailActivity.class));
//            if (!SkipUtil.skipWithNewsItem(context, channelBean)) {
//                Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
//            }
//        }
//    };

    private void onBannerItemClick(NewsItem channelBean) {

        pairs.add(new Pair<>("位置", "大图推荐"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));
        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", channelBean.getNewsTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", channelBean.getNewsTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        if (!SkipUtil.skipWithNewsItem(context, channelBean, map)) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public int onIResume() {
//        if (flowView != null && lifeMode > 0) {
//            JLog.e("aaa will call flow start");
//            flowView.start();
//        }
        return 0;
    }

    @Override
    public int onIPause() {
//        if (flowView != null) {
//            JLog.e("aaa will call flow pause");
//            flowView.pause();
//        }
        return 0;
    }

    @Override
    public int onIDestory() {
        context = null;
        return 0;
    }

    @Override
    public void setHasInManager(boolean inManager) {
        this.hasInManager = inManager;
    }

    @Override
    public boolean hasInManager() {
        return hasInManager;
    }

    @Override
    public void setLifeMode(int mode) {
        this.lifeMode = mode;
    }

}

